package com.asm.handler;

import com.asm.message.ChatRequestMessage;
import com.asm.message.ChatResponseMessage;
import com.asm.server.session.SessionFactory;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
@ChannelHandler.Sharable  //如果不加注解，是不能复用的
public class ChatRequestMessageHandler extends SimpleChannelInboundHandler<ChatRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ChatRequestMessage msg) throws Exception {
        String to = msg.getTo();
        Channel channel = SessionFactory.getSession().getChannel(to);
        if(channel!=null){
            //在线

            channel.writeAndFlush(new ChatResponseMessage(msg.getFrom(), msg.getContent()));
        }else {
            //不在线
            channel.writeAndFlush(new ChatResponseMessage(false,"用户不存在或者不在线"));
        }
    }
}
